<?php
class Reale_Model_RequirementMapper
{
	
	/**
     * @var Zend_Db_Table_Abstract
     */
    protected $_dbTable;

    /**
     * Specify Zend_Db_Table instance to use for data operations
     * 
     * @param  Zend_Db_Table_Abstract $dbTable 
     * @return Reale_Model_RequirementMapper
     */
    public function setDbTable($dbTable)
    {
        if (is_string($dbTable)) {
            $dbTable = new $dbTable();
        }
        if (!$dbTable instanceof Zend_Db_Table_Abstract) {
            throw new Exception('Invalid table data gateway provided');
        }
        $this->_dbTable = $dbTable;
        return $this;
    }

    /**
     * Get registered Zend_Db_Table instance
     *
     * Lazy loads Reale_Model_DbTable_Requirement if no instance registered
     * 
     * @return Zend_Db_Table_Abstract
     */
    public function getDbTable()
    {
        if (null === $this->_dbTable) {
            $this->setDbTable('Reale_Model_DbTable_Requirement');
        }
        return $this->_dbTable;
    }
    /**
     * 
     * Enter description here ...
     * @param Reale_Model_Requirement $req
     */
    public function save($req){
    	$data = array(
    					'category_id' => $req->getCategory_id(),
    					'looking_for' => $req->getLooking_for(),
    					'enquiry' => $req->getEnquiry()
    				);
    	if($req->getAgent_id()){
    		$data['agent_id'] = $req->getAgent_id();
    	}
    	else{
    		$data['name'] = $req->getName();
    		$data['contact_no'] = $req->getContact_no();
    		$data['email'] = $req->getEmail();	
    	}
    	
    	$pKey = $this->getDbTable()->insert($data);
    	
    	if($pKey){
    		$req->setRequirement_id($pKey);
			return true;
    	}
    	return false;
    }
    
	/**
     * 
     * Fetches requirements.
     * @param Reale_Model_Requirements
     * @return Array|NULL
     */
    public function fetchAll($req,$count=null){
    	$select = $this->getDbTable()->getAdapter()
    				->select()
    				->from('requirement',
    						array('requirement_id',
    								'name',
    								'contact_no', 
    								'email',
    								'category_id',
    								'looking_for',
    								'enquiry',
    								'agent_id')
    						)
    				->order('requirement_id DESC')
    				->where(('status = 0'));		
    	if ($count) {
    		$select->limit($count);
    	}
    	return $select->query()
    					   ->fetchAll();
	}
	/**
     * 
     * @param Reale_Model_Requirements
     * @return boolean
     */
	public function acknowledge($req){
		$data = array(
					'status'=>1
					);
		$where = 'requirement_id ='.$req->getRequirement_id();
		return $this->getDbTable()->update($data, $where);	
    }
}
?>